/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "VertexShader.h"
#include <tinyxml.h>
#include "..\Managers\DX11Manager.h"
#include <D3Dcommon.h>
#include <D3D11.h>
namespace Hogshead
{
	namespace DX11Rendering
	{
		ContentCPP(Hogshead::DX11Rendering::VertexShader, VertexShader)
		VertexShader::VertexShader(void)
		{
			_input_layout = NULL;
			_vs = NULL;

			_format_dic.add("DXGI_FORMAT_UNKNOWN", DXGI_FORMAT_UNKNOWN);
			_format_dic.add("DXGI_FORMAT_R32G32B32A32_TYPELESS", DXGI_FORMAT_R32G32B32A32_TYPELESS );
			_format_dic.add("DXGI_FORMAT_R32G32B32A32_FLOAT", DXGI_FORMAT_R32G32B32A32_FLOAT );
			_format_dic.add("DXGI_FORMAT_R32G32B32A32_UINT", DXGI_FORMAT_R32G32B32A32_UINT );
			_format_dic.add("DXGI_FORMAT_R32G32B32A32_SINT", DXGI_FORMAT_R32G32B32A32_SINT );
			_format_dic.add("DXGI_FORMAT_R32G32B32_TYPELESS", DXGI_FORMAT_R32G32B32_TYPELESS );
			_format_dic.add("DXGI_FORMAT_R32G32B32_FLOAT", DXGI_FORMAT_R32G32B32_FLOAT );
			_format_dic.add("DXGI_FORMAT_R32G32B32_UINT", DXGI_FORMAT_R32G32B32_UINT  );
			_format_dic.add("DXGI_FORMAT_R32G32B32_SINT", DXGI_FORMAT_R32G32B32_SINT  );
			_format_dic.add("DXGI_FORMAT_R16G16B16A16_TYPELESS", DXGI_FORMAT_R16G16B16A16_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R16G16B16A16_FLOAT", DXGI_FORMAT_R16G16B16A16_FLOAT  );
			_format_dic.add("DXGI_FORMAT_R16G16B16A16_UNORM", DXGI_FORMAT_R16G16B16A16_UNORM   );
			_format_dic.add("DXGI_FORMAT_R16G16B16A16_UINT", DXGI_FORMAT_R16G16B16A16_UINT  );
			_format_dic.add("DXGI_FORMAT_R16G16B16A16_SNORM", DXGI_FORMAT_R16G16B16A16_SNORM  );
			_format_dic.add("DXGI_FORMAT_R16G16B16A16_SINT", DXGI_FORMAT_R16G16B16A16_SINT  );
			_format_dic.add("DXGI_FORMAT_R32G32_TYPELESS", DXGI_FORMAT_R32G32_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R32G32_FLOAT", DXGI_FORMAT_R32G32_FLOAT  );
			_format_dic.add("DXGI_FORMAT_R32G32_UINT", DXGI_FORMAT_R32G32_UINT  );
			_format_dic.add("DXGI_FORMAT_R32G32_SINT", DXGI_FORMAT_R32G32_SINT  );
			_format_dic.add("DXGI_FORMAT_R32G8X24_TYPELESS", DXGI_FORMAT_R32G8X24_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_D32_FLOAT_S8X24_UINT", DXGI_FORMAT_D32_FLOAT_S8X24_UINT  );
			_format_dic.add("DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS", DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_X32_TYPELESS_G8X24_UINT", DXGI_FORMAT_X32_TYPELESS_G8X24_UINT  );
			_format_dic.add("DXGI_FORMAT_R10G10B10A2_TYPELESS", DXGI_FORMAT_R10G10B10A2_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R10G10B10A2_UNORM", DXGI_FORMAT_R10G10B10A2_UNORM   );
			_format_dic.add("DXGI_FORMAT_R10G10B10A2_UINT", DXGI_FORMAT_R10G10B10A2_UINT   );
			_format_dic.add("DXGI_FORMAT_R11G11B10_FLOAT", DXGI_FORMAT_R11G11B10_FLOAT  );
			_format_dic.add("DXGI_FORMAT_R8G8B8A8_TYPELESS", DXGI_FORMAT_R8G8B8A8_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R8G8B8A8_UNORM", DXGI_FORMAT_R8G8B8A8_UNORM  );
			_format_dic.add("DXGI_FORMAT_R8G8B8A8_UNORM_SRGB", DXGI_FORMAT_R8G8B8A8_UNORM_SRGB  );
			_format_dic.add("DXGI_FORMAT_R8G8B8A8_UINT",  DXGI_FORMAT_R8G8B8A8_UINT  );
			_format_dic.add("DXGI_FORMAT_R8G8B8A8_SNORM", DXGI_FORMAT_R8G8B8A8_SNORM  );
			_format_dic.add("DXGI_FORMAT_R8G8B8A8_SINT", DXGI_FORMAT_R8G8B8A8_SINT  );
			_format_dic.add("DXGI_FORMAT_R16G16_TYPELESS", DXGI_FORMAT_R16G16_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R16G16_FLOAT", DXGI_FORMAT_R16G16_FLOAT  );
			_format_dic.add("DXGI_FORMAT_R16G16_UNORM", DXGI_FORMAT_R16G16_UNORM  );
			_format_dic.add("DXGI_FORMAT_R16G16_UINT", DXGI_FORMAT_R16G16_UINT  );
			_format_dic.add("DXGI_FORMAT_R16G16_SNORM", DXGI_FORMAT_R16G16_SNORM  );
			_format_dic.add("DXGI_FORMAT_R16G16_SINT", DXGI_FORMAT_R16G16_SINT  );
			_format_dic.add("DXGI_FORMAT_R32_TYPELESS", DXGI_FORMAT_R32_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_D32_FLOAT", DXGI_FORMAT_D32_FLOAT  );
			_format_dic.add("DXGI_FORMAT_R32_FLOAT", DXGI_FORMAT_R32_FLOAT  );
			_format_dic.add("DXGI_FORMAT_R32_UINT", DXGI_FORMAT_R32_UINT  );
			_format_dic.add("DXGI_FORMAT_R32_SINT", DXGI_FORMAT_R32_SINT  );
			_format_dic.add("DXGI_FORMAT_R24G8_TYPELESS", DXGI_FORMAT_R24G8_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_D24_UNORM_S8_UINT", DXGI_FORMAT_D24_UNORM_S8_UINT  );
			_format_dic.add("DXGI_FORMAT_R24_UNORM_X8_TYPELESS", DXGI_FORMAT_R24_UNORM_X8_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_X24_TYPELESS_G8_UINT", DXGI_FORMAT_X24_TYPELESS_G8_UINT  );
			_format_dic.add("DXGI_FORMAT_R8G8_TYPELESS", DXGI_FORMAT_R8G8_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R8G8_UNORM", DXGI_FORMAT_R8G8_UNORM  );
			_format_dic.add("DXGI_FORMAT_R8G8_UINT", DXGI_FORMAT_R8G8_UINT  );
			_format_dic.add("DXGI_FORMAT_R8G8_SNORM", DXGI_FORMAT_R8G8_SNORM  );
			_format_dic.add("DXGI_FORMAT_R8G8_SINT", DXGI_FORMAT_R8G8_SINT  );
			_format_dic.add("DXGI_FORMAT_R16_TYPELESS", DXGI_FORMAT_R16_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R16_FLOAT", DXGI_FORMAT_R16_FLOAT  );
			_format_dic.add("DXGI_FORMAT_D16_UNORM", DXGI_FORMAT_D16_UNORM  );
			_format_dic.add("DXGI_FORMAT_R16_UNORM", DXGI_FORMAT_R16_UNORM  );
			_format_dic.add("DXGI_FORMAT_R16_UINT", DXGI_FORMAT_R16_UINT  );
			_format_dic.add("DXGI_FORMAT_R16_SNORM", DXGI_FORMAT_R16_SNORM  );
			_format_dic.add("DXGI_FORMAT_R16_SINT", DXGI_FORMAT_R16_SINT  );
			_format_dic.add("DXGI_FORMAT_R8_TYPELESS", DXGI_FORMAT_R8_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_R8_UNORM", DXGI_FORMAT_R8_UNORM  );
			_format_dic.add("DXGI_FORMAT_R8_UINT", DXGI_FORMAT_R8_UINT  );
			_format_dic.add("DXGI_FORMAT_R8_SNORM", DXGI_FORMAT_R8_SNORM  );
			_format_dic.add("DXGI_FORMAT_R8_SINT", DXGI_FORMAT_R8_SINT  );
			_format_dic.add("DXGI_FORMAT_A8_UNORM", DXGI_FORMAT_A8_UNORM  );
			_format_dic.add("DXGI_FORMAT_R1_UNORM", DXGI_FORMAT_R1_UNORM  );
			_format_dic.add("DXGI_FORMAT_R9G9B9E5_SHAREDEXP", DXGI_FORMAT_R9G9B9E5_SHAREDEXP  );
			_format_dic.add("DXGI_FORMAT_R8G8_B8G8_UNORM", DXGI_FORMAT_R8G8_B8G8_UNORM  );
			_format_dic.add("DXGI_FORMAT_G8R8_G8B8_UNORM", DXGI_FORMAT_G8R8_G8B8_UNORM  );
			_format_dic.add("DXGI_FORMAT_BC1_TYPELESS", DXGI_FORMAT_BC1_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_BC4_UNORM", DXGI_FORMAT_BC4_UNORM  );
			_format_dic.add("DXGI_FORMAT_BC4_SNORM", DXGI_FORMAT_BC4_SNORM  );
			_format_dic.add("DXGI_FORMAT_BC5_TYPELESS", DXGI_FORMAT_BC5_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_BC5_UNORM", DXGI_FORMAT_BC5_UNORM  );
			_format_dic.add("DXGI_FORMAT_BC5_SNORM", DXGI_FORMAT_BC5_SNORM  );
			_format_dic.add("DXGI_FORMAT_B5G6R5_UNORM", DXGI_FORMAT_B5G6R5_UNORM  );
			_format_dic.add("DXGI_FORMAT_B5G5R5A1_UNORM", DXGI_FORMAT_B5G5R5A1_UNORM  );
			_format_dic.add("DXGI_FORMAT_B8G8R8A8_UNORM", DXGI_FORMAT_B8G8R8A8_UNORM  );
			_format_dic.add("DXGI_FORMAT_B8G8R8X8_UNORM", DXGI_FORMAT_B8G8R8X8_UNORM  );
			_format_dic.add("DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM", DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM  );
			_format_dic.add("DXGI_FORMAT_B8G8R8A8_TYPELESS", DXGI_FORMAT_B8G8R8A8_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_B8G8R8A8_UNORM_SRGB", DXGI_FORMAT_B8G8R8A8_UNORM_SRGB  );
			_format_dic.add("DXGI_FORMAT_B8G8R8X8_TYPELESS", DXGI_FORMAT_B8G8R8X8_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_B8G8R8X8_UNORM_SRGB", DXGI_FORMAT_B8G8R8X8_UNORM_SRGB  );
			_format_dic.add("DXGI_FORMAT_BC6H_TYPELESS", DXGI_FORMAT_BC6H_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_BC6H_UF16", DXGI_FORMAT_BC6H_UF16  );
			_format_dic.add("DXGI_FORMAT_BC6H_SF16", DXGI_FORMAT_BC6H_SF16  );
			_format_dic.add("DXGI_FORMAT_BC7_TYPELESS", DXGI_FORMAT_BC7_TYPELESS  );
			_format_dic.add("DXGI_FORMAT_BC7_UNORM", DXGI_FORMAT_BC7_UNORM  );
			_format_dic.add("DXGI_FORMAT_BC7_UNORM_SRGB", DXGI_FORMAT_BC7_UNORM_SRGB  );
			_format_dic.add("DXGI_FORMAT_FORCE_UINT", DXGI_FORMAT_FORCE_UINT  );

			_input_slot_class_dic.add("D3D11_INPUT_PER_VERTEX_DATA", D3D11_INPUT_PER_VERTEX_DATA );
			_input_slot_class_dic.add("D3D11_INPUT_PER_INSTANCE_DATA", D3D11_INPUT_PER_INSTANCE_DATA);
		}


		VertexShader::~VertexShader(void)
		{
			
		}

		void VertexShader::finalize()
		{
			if(_input_layout) _input_layout->Release();
			if(_vs) _vs->Release();
		}

		void VertexShader::explicitLoad()
		{
			error("You cannot load a VertexShader in this manor, XML is required");
		}

		void VertexShader::readXML(const String& in_xml)
		{
			//parse the String for what we need
			TiXmlDocument document;
			document.Parse(in_xml);

			TiXmlElement* root_element = document.RootElement();
			String root_tag(root_element->Value());

			if(root_tag.equals("Content"))
			{
				//get the standard stuff
				
				this->_key = String(root_element->Attribute("key"));
				this->_path = String(root_element->Attribute("path"));

				//used to get the absolute path to it 
				this->_path = ContentLibrary::getCompleteContentString( this->_path );

				//get the shader specific stuff
				
				HRESULT h_result = S_OK;
				ID3DBlob* vs_blob = NULL;
				//the key is the name of the shader, must match with file
				h_result = Shader::CompileShaderFromFile(this->_path, root_element->Attribute("entry"), root_element->Attribute("model"), &vs_blob);

				if( h_result != S_OK )
				{
					MessageBox( NULL, 
						"The Shader file cannot be compiled.",
						this->_path, 
						MB_OK);

					//release blob
 					vs_blob->Release();
					return;
				}

				if(root_element->NoChildren())
				{
					StringBuilder sb;
					sb << "Vertex Shader: " << this->_path << " : missing input layout";
					error(sb);
				}
				else
				{
					TiXmlElement* input_layout_element = root_element->FirstChild()->ToElement();
					String input_layout_tag(input_layout_element->Value());

					//make sure we have a tag for input layout
					if(input_layout_tag.equals("Input_Layout"))
					{
						TiXmlNode* child_node = NULL;
						TiXmlElement* child_element;

						//iterate through each element of the input layout desc
						UINT number_elements = 0;
						Vector<D3D11_INPUT_ELEMENT_DESC*> input_desc;

						while(child_node = input_layout_element->IterateChildren(child_node))
						{
							child_element = child_node->ToElement();
							if(!child_element)
								continue;

							//go through the element tags to create our input layout
							String tag(child_element->Value());
							if(tag.equals("Input_Layout_Element"))
							{
								D3D11_INPUT_ELEMENT_DESC* desc = new D3D11_INPUT_ELEMENT_DESC();

								const char* semantic_name = child_element->Attribute("name");
								if(semantic_name)
									desc->SemanticName = semantic_name;
								else
									error(StringBuilder() << "Input layout does not have a semantic name: " << this->_path);
		
								int semantic_index = 0;
								int return_val = child_element->QueryIntAttribute("semanticIndex", &semantic_index);
								if(return_val != TIXML_NO_ATTRIBUTE)
									desc->SemanticIndex = semantic_index;
								else
									error(StringBuilder() << "Input layout does not have a semantic index: " << this->_path);

								const char* format = child_element->Attribute("format");
								if(format)
								{
									DXGI_FORMAT format_val;
									if(_format_dic.tryGetValue(format, format_val))
										desc->Format = format_val;
									else
										error(StringBuilder() << "Unknown format: " << format << endl << "path: " << this->_path);
								}
								else
									error(StringBuilder() << "Input layout does not have a format: " << this->_path);

								int input_slot = 0;
								return_val = child_element->QueryIntAttribute("inputSlot", &input_slot);
								if(return_val != TIXML_NO_ATTRIBUTE)
									desc->InputSlot = input_slot;
								else
									error(StringBuilder() << "Input layout does not have an input slot: " << this->_path);

								const char* aligned_byte_offset = child_element->Attribute("alignedByteOffset");
								if(aligned_byte_offset)
								{
									int aligned_byte_offset_int = 0;
									if(Integer::tryParse(aligned_byte_offset, aligned_byte_offset_int))
										desc->AlignedByteOffset = aligned_byte_offset_int;
									else if(String::equals(aligned_byte_offset, "D3D11_APPEND_ALIGNED_ELEMENT"))
										desc->AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
								}
								else
									error(StringBuilder() << "Input layout does not have an aligned byte offset: " << this->_path);

								const char* input_slot_class = child_element->Attribute("inputSlotClass");
								if(input_slot_class)
									desc->InputSlotClass = _input_slot_class_dic[input_slot_class];
								else
									error(StringBuilder() << "Input layout does not have an input slot class: " << this->_path);

								int instance_data_step_rate = 0;
								child_element->QueryIntAttribute("instanceDataStepRate", &instance_data_step_rate);
								if(return_val != TIXML_NO_ATTRIBUTE)
									desc->InstanceDataStepRate = instance_data_step_rate;
								else
									error(StringBuilder() << "Input layout does not have an instance data step rate: " << this->_path);

								input_desc.add(desc);
							}
						}

						Array1D<D3D11_INPUT_ELEMENT_DESC> input_desc_array(input_desc.size());
						for(int k = 0; k < input_desc_array.length(); k++)
						{
							input_desc_array(k) = *input_desc[k];
							delete input_desc[k];
						}

						//create input layout
						//_input_layout = DX11Manager::createLayout((const D3D11_INPUT_ELEMENT_DESC*)input_desc, input_desc.size(), vs_blob);
						_input_layout = DX11Manager::createLayout((const D3D11_INPUT_ELEMENT_DESC*)input_desc_array, input_desc_array.length(), vs_blob);

						//create shader
						h_result = DX11Manager::createVSFromBlob(vs_blob, &_vs);
						if( FAILED( h_result ) )
						{
							MessageBox( NULL, 
							"The Shader file could not be created from byte code.",
							this->_path, 
							MB_OK);
						
							//release blob
							vs_blob->Release();
							return;
						}
					}
				}
#ifdef _DEBUG
				HRESULT hr = _input_layout->SetPrivateData( WKPDID_D3DDebugObjectName, _path.length() - 1, _path );
				if(FAILED(hr))
				{
					StringBuilder sb;
					sb << "ERROR ON LOAD: Vertex Shader could not be named: " << _path;
					DX11Manager::_logger->write(sb);
					error(sb);
				}

				hr = _vs->SetPrivateData( WKPDID_D3DDebugObjectName, _path.length() - 1, _path );
				if(FAILED(hr))
				{
					StringBuilder sb;
					sb << "ERROR ON LOAD: Vertex Shader could not be named: " << _path;
					DX11Manager::_logger->write(sb);
					error(sb);
				}
#endif
				//release blob
				vs_blob->Release();
				return;
			}
			ContentLibrary::write("XML Schema error in:");
			ContentLibrary::write(this->_path);
			error("Invalid VertexShader xml file");
			return;
		}
	}
}